<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetNotifyTemplates;

use App\Base\Resources\BaseAdminResource;
use App\Helpers\ArrayHelper;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigs;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigsConfig;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;
use App\Plugins\FcpApi\TweetPluginUsages\TweetPluginUsagesConfig;
use App\Plugins\FcpApi\TweetPluginUsages\TweetPluginUsagesService;

class TweetConfigsResource extends BaseAdminResource
{
    public function toArray($request)
    {
        // form 字段
        $formMap = TweetConfigsConfig::FORM_FIELDS_MAP;
        $formMapFieldsArr = [];
        foreach ($formMap as $k => $dbField) {
            $formMapFieldsArr[$dbField] = $this->$dbField;
        }

        $itemValueArr = json_decode($this->item_value, true);
        $channelArr = [];
        $emailArr = [];
        $smsArr = [];
        $emailStatus = false;
        $smsStatus = false;
        foreach ($itemValueArr as $v) {
            if ($v['isEnable'] == 'true' && $v['type'] == 'email') {
                $emailStatus = true;
            }
            if ($v['type'] == 'email') {
                $emailArr = $v['template'];
                $channelArr[] = ['key' => 2, 'value'=>2, 'name' => '邮件',  'title' => '邮件', 'icon' => 'icon-email'];
            }
            if ($v['isEnable'] == 'true' && $v['type'] == 'sms') {
                $smsStatus = true;
            }
            if ($v['type'] == 'sms') {
                $smsArr = $v['template'];
                $channelArr[] = ['key' => 3, 'value'=>3, 'name' => '短信',  'title' => '短信', 'icon' => 'icon-duanxin'];
            }
        }
        //  dump($itemValueArr);
        //  多语言
        $languageArr = TweetConfigService::getLanguageStatus();
        $newEmailArr = $languageArr['languagesOption'];
        $newSmsArr = $languageArr['languagesOption'];
        foreach ($newEmailArr as &$f) {
            $f['langTag'] = $f['key'];
            foreach ($emailArr as $r) {
                if ($f['key'] == $r['langTag']) {
                    $f['title'] = $r['title'] ?? '';
                    $f['content'] = $r['content'] ?? '';
                }
            }
        }
        foreach ($newSmsArr as &$f) {
            $f['langTag'] = $f['key'];
            foreach ($smsArr as $r) {
                if ($f['key'] == $r['langTag']) {
                    $f['templateCode'] = $r['templateCode'] ?? '';
                    $f['codeParam'] = $r['codeParam'] ?? '';
                    $f['signName'] = $r['signName'] ?? '';
                }
            }
        }
        switch ($this->item_key) {
            case 'verifycode_template1':
                $description = '通用验证码';
                $description_url = 'systemsms.base.Universal_verification_code';
                $key = 1;
                break;
            case 'verifycode_template2':
                $description = '注册新账号';
                $description_url = 'systemsms.base.Register_new_account';
                $key = 2;
                break;
            case 'verifycode_template3':
                $description = '修改账号资料';
                $description_url = 'systemsms.base.Modify_account_information';
                $key = 3;
                break;
            case 'verifycode_template4':
                $description = '更换新绑定';
                $description_url = 'systemsms.base.Replace_new_binding';
                $key = 4;
                break;
            case 'verifycode_template5':
                $description = '重置登录密码';
                $description_url = 'systemsms.base.Reset_login_password';
                $key = 5;
                break;
            case 'verifycode_template6':
                $description = '重置支付密码';
                $description_url = 'systemsms.base.Reset_payment_password';
                $key = 6;
                break;
            default:
                $description = '使用验证码登录';
                $description_url = 'systemsms.base.Login_with_verification_code';
                $key = 7;
                break;
        }
        // 默认字段
        $default = [
            'key'       => $key,
            'id'        => $this->id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'description' => $description_url,
            'description_url' => $description_url,
            'channel' => $channelArr,
            'smsStatus' => $smsStatus,
            'emailStatus' => $emailStatus,
            'item_key' => $this->item_key,
            'item_value_decode'  => $itemValueArr,
            'newEmailArr' => $newEmailArr,
            'newSmsArr' => $newSmsArr,
        ];

        // 合并
        $arr = array_merge($formMapFieldsArr, $default);

        return $default;
    }
}
